<?php

namespace app\admin\service;


use app\common\model\integral\IntegralProductAttribute;
use app\common\model\integral\IntegralProductSpec;

class IntegralProductAttributeService extends BaseService
{

    public function __construct()
    {
        $this->model = new IntegralProductAttribute();
    }


    public function edit()
    {
        $data = request()->param();
        $list = getter($data, 'ids', []);
        if (empty($list)) {
            return message('关联规格不能为空！', false);
        }

        $data['spec_ids'] = implode(',', $list);
        $rowId = $this->model->edit($data);
        if ($rowId) {
            return message();
        }
        return message('错误', false);
    }



    public function getList()
    {
        $map = [];
        $title = getter(request()->param(), 'title', '');
        !empty($title) && $map[] = ['title', '=', $title];
        $list = $this->model->where($map)->page(PAGE, PERPAGE)->select();
        foreach ($list as $k => $v){
            $ids = explode(',', $v['spec_ids']);
            $list[$k]['spec_value_name'] = IntegralProductSpec::where('id', 'in', $ids)->field('group_concat(title) as title')->find()->toArray()['title'];
            foreach ($ids as &$vl){
                $vl = $vl*1;
            }
            $list[$k]['ids'] = $ids;
        }


        //获取数据总数
        $count = $this->model->where($map)->count();

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }


}